package com.changgou.goods.controller;

import com.changgou.goods.service.GoodsService;
import com.changgou.pojo.Brand;
import com.changgou.pojo.Goods;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("/goods")
@CrossOrigin
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @GetMapping
    public Result<Goods> findAll(){
        Result<Goods> goodsList = goodsService.findAll();
        return new Result<Goods>(true, StatusCode.OK,"查询成功",goodsList);
    }

    @GetMapping("/{id}")
    public Result<Goods> findById(@PathVariable Integer id){
        Result<Goods> goods = goodsService.findById(id);
        return new Result<Goods>(true, StatusCode.OK,"查询成功",goods);
    }

    @PostMapping
    public Result add(@RequestBody Goods goods){
        goodsService.add(goods);
        return new Result(true,StatusCode.OK,"添加成功");
    }



    @PutMapping(value = "/{id}")
    public Result update(@RequestBody Goods goods, @PathVariable Integer id){
        //设置ID
        goods.setId(id);
        //修改数据
        goodsService.updateByPrimaryKey(goods);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Integer id){
        goodsService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /***
     * 多条件搜索品牌数据
     * @param goods
     * @return
     */
    @PostMapping(value = "/search" )
    public List<Goods> findList(@RequestBody(required = false) Goods goods){
        List<Goods> list = goodsService.findList(goods);
        return (List<Goods>) new Result<List<Brand>>(true,StatusCode.OK,"查询成功",list);
    }
}
